import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login.vue'
import Main from '@/views/Home.vue'
import GoodsEdit from '@/views/GoodsEdit.vue'
import GoodsList from '@/views/GoodsList.vue'
import CategoryEdit from '@/views/CategoryEdit.vue'
import CategoryList from '@/views/CategoryList.vue'
import AdEdit from '@/views/AdEdit.vue'
import AdList from '@/views/AdList.vue'
import AdminUserEdit from '@/views/AdminUserEdit.vue'
import AdminUserList from '@/views/AdminUserList.vue'
import OrderList from '@/views/OrderList.vue'
import UserList from '@/views/UserList.vue'
import UserEdit from '@/views/UserEdit.vue'

Vue.use(VueRouter)

  const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      isPublic: true
    }
  },
  {
    path: '/',
    name: 'Main',
    component: Main,
    children:[
      {
        path: '/category/create',
        name: 'CategoryCreate',
        component: CategoryEdit
      },
      {
        path: '/category/edit/:id',
        name: 'CategoryEdit',
        component: CategoryEdit,
        props: true
      },
      {
        path: '/category/list',
        name: 'CategoryList',
        component: CategoryList
      },
      {
        path: '/goods/list',
        name: 'GoodsList',
        component: GoodsList
      },
      {
        path: '/goods/create',
        name: 'GoodsCreate',
        component: GoodsEdit
      },
      {
        path: '/goods/edit/:id',
        name: 'GoodsEdit',
        component: GoodsEdit,
        props: true
      },
      {
        path: '/ads/list',
        name: 'AdList',
        component: AdList
      },
      {
        path: '/ads/create',
        name: 'AdCreate',
        component: AdEdit
      },
      {
        path: '/ads/edit/:id',
        name: 'AdEdit',
        component: AdEdit,
        props: true
      },
      {
        path: '/admin_users/list',
        name: 'AdminUserList',
        component: AdminUserList
      },
      {
        path: '/admin_users/create',
        name: 'AdminUserCreate',
        component: AdminUserEdit
      },
      {
        path: '/admin_users/edit/:id',
        name: 'AdminUserEdit',
        component: AdminUserEdit,
        props: true
      },
      {
        path: '/order/list',
        name: 'OrderList',
        component: OrderList,
      },
      {
        path: '/user/list',
        name: 'UserList',
        component: UserList,
      },
      {
        path: '/user/edit/:id',
        name: 'UserEdit',
        component: UserEdit,
        props: true
      },
    ]
  },
]


const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (!to.meta.isPublic && !sessionStorage.token) {
    return next('/login')
  }
  next()
})

export default router
